/*
 
Enterprise Social Network (ESN) is an open-source Java EE framework for
a social network application.

Copyright (C) 2006-07 (see CONTRIBUTORS file in project root)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

package esn.core.ejb.entity;

import esn.core.util.*;
import java.io.*;
import java.util.*;
import javax.persistence.*;

/**
 * <p>Representation of user authentication information used by
 * JAAS. Passwords are stored using SHA-1 hash.</p>
 * 
 * @author Anirban Basu
 *
 */
@Entity
public class UserAuthentication implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private String userId;
	private String passwordDigest;
	private Collection<UserRole> roles = new ArrayList<UserRole>(); 
	
	/**
	 * Accessor for password digest.
	 * 
	 * @return
	 */
	@Column(nullable=false, length=2048)
	public String getPasswordDigest() {
		return passwordDigest;
	}
	
	/**
	 * Mutator for password digest.
	 * 
	 * @param passwordDigest
	 */
	public void setPasswordDigest(String passwordDigest) {
		this.passwordDigest = passwordDigest;
	}
	
	/**
	 * Hashes a clear text password using SHA-1 and stores it.
	 * 
	 * @param password clear text password
	 * @throws Exception
	 */
	public void digestPassword(String password) {
		this.passwordDigest = MessageDigestHelper.generateHexSHA1Digest(password);
	}
	
	/**
	 * Accessor for the primary key.
	 * 
	 * @return
	 */
	@Id
	public String getUserId() {
		return userId;
	}
	
	/**
	 * Mutator for the primary key.
	 * 
	 * @param userId
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * Accessor for user roles object.
	 * 
	 * @return
	 */
	@OneToMany(mappedBy="userAuthentication",
			cascade={CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.REMOVE})
	public Collection<UserRole> getRoles() {
		return roles;
	}

	/**
	 * Mutator for user roles object.
	 * 
	 * @param roles
	 */
	public void setRoles(Collection<UserRole> roles) {
		this.roles = roles;
	}

	
}
